<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/2/24
 * Time: 10:38
 */

namespace app\console\controller\option;

use app\common\controller\Console;
use think\Cache;

class Domain extends Console {

    public function index() {
        $status = request()->param('status', null);

        $where = [];
        if (null !== $status) {
            $where["status"] = $status;
        }

        $model = model('Domains');
        $list = $model->where($where)->order('modified desc')->paginate(15);

        $count = [
            'all' => $model->where([])->count("domain_id"),
            'enabled' => $model->where(["status" => 1])->count("domain_id"),
            'disabled' => $model->where(["status" => 0])->count("domain_id"),
        ];

        $this->assign([
            'list' => $list,
            'count' => $count,
        ]);

        cookie('_url_', request()->url());

        return $this->fetch();
    }

    public function add() {
        return $this->fetch();
    }

    public function insert() {
        $name = request()->param('name');
        $status = request()->param('status');
        $type = request()->param('type');
        $data = [
            'name' => $name,
            'status' => $status,
            'type'  => $type,
            'add_time' => time(),
            'modified' => time(),
        ];
        $domain_id = model('Domains')->insert($data,false,true);
        $this->update_cache($type);
        $this->success("操作成功。");
    }

    public function edit() {
        $domain_id = request()->param('id');

        $row = model('Domains')->where("domain_id", $domain_id)->find();
        $this->assign('row', $row);
        return $this->fetch();
    }

    public function update() {
        $domain_id = request()->param('domain_id');
        $name = request()->param('name');
        $status = request()->param('status');
        $type = request()->param('type');
        $data = [
            'name' => $name,
            'status' => $status,
            'type'  => $type,
            'modified' => time(),
        ];
        model('Domains')->update($data,["domain_id" => $domain_id]);
        $this->update_cache($type);
        $this->success("操作成功。");
    }

    public function delete() {
        $domain_id = request()->param('id');
        $model = model('Domains');
        $type = $model->where("domain_id", $domain_id)->value('type');
        $model->where("domain_id", $domain_id)->delete();
        $this->update_cache($type);
        $this->success("操作成功。");
    }

    public function change($id, $target_status)
    {
        if (!in_array($target_status, [1,0])) {
            abort(404, '错误请求');
        }
        $model = model('Domains');
        $model->where("domain_id", $id)->update(['status' => $target_status]);
        $type = $model->where("domain_id", $id)->value('type');
        $this->update_cache($type);
        return $this->redirect($_SERVER['HTTP_REFERER']);
    }
    
    private function update_cache($type)
    {
        $name = 'domain_type' . $type;
        $model = model('Domains');
        $res = $model->field('name')->where(['type' =>$type, 'status' => 1])->select();
        $domains = [];
        foreach ($res as $v) {
            $domains[] = $v['name'];
        }
        Cache::store('redis')->set($name, serialize($domains));
    }
}
